//
//  SCRequest.m
//  SmoCash
//
//  Created by mac on 2025/5/8.
//

#import "SCRequest.h"
#import "DataFrameParam.h"
@interface AFHttpClient : AFHTTPSessionManager

+ (instancetype)sharedClient;

@end
static const NSTimeInterval RequestTimeoutInterval = 15;

@implementation AFHttpClient

+ (instancetype)sharedClient {
    
    static AFHttpClient *client = nil;
    
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        
        NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
        
        client = [[AFHttpClient alloc] initWithSessionConfiguration:configuration];
        
        client.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json", @"text/html", @"text/plain", @"text/gif", nil];
        
//        client.requestSerializer =  [AFJSONRequestSerializer serializer];


        client.requestSerializer.timeoutInterval = RequestTimeoutInterval;
        
        client.securityPolicy = [AFSecurityPolicy defaultPolicy];
        
    });
    
    return client;
}

@end

@implementation SCRequest
+ (void)frameMoverWifi:(void (^)(void))success {
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        switch (status) {
            case AFNetworkReachabilityStatusReachableViaWWAN:
                success();
                break;
            case AFNetworkReachabilityStatusReachableViaWiFi:
                success();
                break;
            case AFNetworkReachabilityStatusNotReachable:
                break;
            case AFNetworkReachabilityStatusUnknown:
                break;
            default:
                break;
        }
    }];
}


+ (void)postHUDWithUrl:(NSString *)url params:(nullable NSDictionary *)params isMsg:(BOOL)isMsg success:(nullable HttpSuccessBlock)success failure:(nullable HttpFailureBlock)failure {
    AFJSONResponseSerializer *response = [AFJSONResponseSerializer serializer];
    [[AFHttpClient sharedClient] setResponseSerializer:response];
    int tag = arc4random() % 99999;
    [MBProgressHUD showMessage:@"" ToView:nil Tag:tag];
    NSString *ps = [NSString stringWithFormat:@"%@%@%@",HtpMac,url,[DataFrameParam frameMoverInfoPart]];;
    [[AFHttpClient sharedClient] POST:ps parameters:params headers:nil progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [MBProgressHUD hideHUDForView:nil Tag:tag];
            if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [MBProgressHUD hideHUDForView:nil Tag:tag];
//        [MBProgressHUD showAutoMessage:error.localizedDescription];
        if (failure) {
            failure(error);
        }
        
    }];
}
+ (void)getWithUrl:(NSString *)url params:(nullable NSDictionary *)params isMsg:(BOOL)isMsg success:(nullable HttpSuccessBlock)success failure:(nullable HttpFailureBlock)failure {
    AFJSONResponseSerializer *response = [AFJSONResponseSerializer serializer];
    [[AFHttpClient sharedClient] setResponseSerializer:response];
    int tag = arc4random() % 99999;
    [MBProgressHUD showMessage:@"" ToView:nil Tag:tag];
    NSMutableDictionary *header = [[NSMutableDictionary alloc]init];
    NSString *ps = [NSString stringWithFormat:@"%@%@%@",HtpMac,url,[DataFrameParam frameMoverInfoPart]];;
    [[AFHttpClient sharedClient] GET:ps parameters:params headers:nil progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [MBProgressHUD hideHUDForView:nil Tag:tag];
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [MBProgressHUD hideHUDForView:nil Tag:tag];
//        [MBProgressHUD showAutoMessage:error.localizedDescription];
        if (failure) {
            NSLog(@"%@",error);
                failure(error);
        }
    }];
}
+ (void)getInitWithUrl:(NSString *)url params:(nullable NSDictionary *)params isMsg:(BOOL)isMsg success:(nullable HttpSuccessBlock)success failure:(nullable HttpFailureBlock)failure {
    AFJSONResponseSerializer *response = [AFJSONResponseSerializer serializer];
    [[AFHttpClient sharedClient] setResponseSerializer:response];
    int tag = arc4random() % 99999;
    [MBProgressHUD showMessage:@"" ToView:nil Tag:tag];
    NSMutableDictionary *header = [[NSMutableDictionary alloc]init];
    NSString *ps = [NSString stringWithFormat:@"%@%@%@",HtpMac,url,[DataFrameParam frameMoverInfoPart]];;
    [[AFHttpClient sharedClient] GET:ps parameters:params headers:nil progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [MBProgressHUD hideHUDForView:nil Tag:tag];
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [MBProgressHUD hideHUDForView:nil Tag:tag];
//        [MBProgressHUD showAutoMessage:error.localizedDescription];
        if (failure) {
            NSLog(@"%@",error);
                failure(error);
        }
    }];
}

+ (void)upImg:(NSString *)url paramers:(id)paramers HUDInView:(UIView *)view file:(UIImage *)file PostsWithBlock:(void (^)(id JSON, NSError *error))block{
    MBProgressHUD *HUD;
    if (view) {
        HUD  = [MBProgressHUD showHUDAddedTo:view animated:YES];
        HUD.userInteractionEnabled = YES;
    }
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"image/jpeg",@"image/png",@"application/octet-stream",nil];
    manager.requestSerializer= [AFHTTPRequestSerializer serializer];
    manager.responseSerializer= [AFJSONResponseSerializer serializer];
    NSString *ps = [NSString stringWithFormat:@"%@%@%@",HtpMac,url,[DataFrameParam frameMoverInfoPart]];;
    [manager POST:ps parameters:paramers headers:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        formatter.dateFormat = @"yyyyMMddHHmmss";
        NSString *str = [formatter stringFromDate:[NSDate date]];
        NSString *fileName = [NSString stringWithFormat:@"%@.jpg", str];
        [formData appendPartWithFileData:UIImageJPEGRepresentation(file, 0.1) name:@"attach" fileName:fileName mimeType:@"image/jpeg"];
    } progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@ %@",url,responseObject);
        [MBProgressHUD hideHUDForView:view animated:NO];
        if (block) {
            block(responseObject,nil);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [MBProgressHUD hideHUDForView:view animated:NO];
        if (block) {
            block(nil,error);
        }
    }];
    
}

+ (void)upData:(NSString *)url paramers:(id)paramers HUDInView:(UIView *)view file:(NSData *)file PostsWithBlock:(void (^)(id JSON, NSError *error))block{
    MBProgressHUD *HUD;
    NSString *ps = [NSString stringWithFormat:@"%@%@%@",HtpMac,url,[DataFrameParam frameMoverInfoPart]];;
    if (view) {
        HUD  = [MBProgressHUD showHUDAddedTo:view animated:YES];
        HUD.userInteractionEnabled = YES;
    }
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"image/jpeg",@"image/png",@"application/octet-stream",nil];
    manager.requestSerializer= [AFHTTPRequestSerializer serializer];
    manager.responseSerializer= [AFJSONResponseSerializer serializer];
    [manager POST:ps parameters:paramers headers:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        [formData appendPartWithFormData:file name:@"happened"];
    } progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@ %@",url,responseObject);
        [MBProgressHUD hideHUDForView:view animated:NO];
        if (block) {
            block(responseObject,nil);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [MBProgressHUD hideHUDForView:view animated:NO];
        if (block) {
            block(nil,error);
        }
    }];
    
}


@end
